package com.av.record.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class InputAudioRecordButton extends TextView {

    private InputAudioRecordView recordView;

    private Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==-1){
                recordView.setVisibility(GONE);
            }
            return false;
        }
    });

    public InputAudioRecordButton(Context context) {
        super(context);
        initViews();
    }

    public InputAudioRecordButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public InputAudioRecordButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews();
    }

    public void setRecordView(InputAudioRecordView recordView) {
        this.recordView = recordView;
        this.recordView.setRecordButton(this);
    }

    private boolean longPress=false;
    private void initViews(){
        setPressState(false);
    }

    private float startY;
    private boolean actionOver=false;

    private long startRecordTimer=0L;
    //最短的录音时间限制
    private final long minRecordTimer=1000L;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            startY=event.getY();
            setPressState(true);
            actionOver=false;
            startRecordTimer=System.currentTimeMillis();
            if(recordView!=null){
                recordView.startRecord();
                recordView.setVisibility(VISIBLE);
            }
            longPress=true;
        }else if(event.getAction()==MotionEvent.ACTION_MOVE){
            if(recordView!=null){
                if(startY-event.getY()>100){
                    //取消
                    recordView.changeRecordState(false);
                }else {
                    recordView.changeRecordState(true);
                }
            }
        }else if(event.getAction()==MotionEvent.ACTION_UP
                ||event.getAction()==MotionEvent.ACTION_CANCEL){
            if(!actionOver) {
                stopRecordByTouch(true,
                        event.getY(),
                        System.currentTimeMillis()-startRecordTimer<minRecordTimer);
            }
        }
        return true;
    }

    private void setPressState(boolean press){
        if(press){
            setText("松开 结束");
            setBackgroundResource(Res.getDrawableID("bg_input_audio_press"));
        }else {
            setText("按住 录音");
            setBackgroundResource(Res.getDrawableID("bg_input_audio_n"));
        }
    }


    /**
     *
     * @param touch 是否为主动抬手
     * @param y 手势抬起的坐标
     */
    private void stopRecordByTouch(boolean touch,float y,boolean timerShort){
        actionOver=true;
        if (recordView != null) {
            if(touch) {
                if(timerShort){
                    recordView.stopRecord(false);
                    recordView.recordTimerShort();
                    handler.sendEmptyMessageDelayed(-1,500);
                }else {
                    if (startY - y > 100) {
                        //取消
                        recordView.stopRecord(false);
                    } else {
                        recordView.stopRecord(true);
                    }
                    recordView.setVisibility(GONE);
                }
            }else {
                recordView.setVisibility(GONE);
            }
        }
        setPressState(false);
    }

    public void recordOver(){
        stopRecordByTouch(false,0,false);
        recordView.setVisibility(GONE);
    }

}
